package models

import (
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

func init()  {
	// 注册数据库
	aliasName := beego.AppConfig.String("alias_name")
	dbType := beego.AppConfig.String("db_type")
	dataSource := fmt.Sprintf(
		"%s:%s@tcp(%s:%s)/%s?charset=utf8",
		beego.AppConfig.String("db_user"),
		beego.AppConfig.String("db_pass"),
		beego.AppConfig.String("db_host"),
		beego.AppConfig.String("db_part"),
		beego.AppConfig.String("db_name"),
	)

	orm.RegisterDataBase(aliasName, dbType, dataSource)
	orm.Debug = false
	// invalid connection
	maxIdleConns := beego.AppConfig.DefaultInt("max_idle_conns", 10)
	maxOpenConns := beego.AppConfig.DefaultInt("max_open_conns", 1000)
	orm.SetMaxIdleConns(aliasName, maxIdleConns)
	orm.SetMaxOpenConns(aliasName, maxOpenConns)
	db, _ := orm.GetDB(aliasName)
	db.SetConnMaxLifetime(50)
}

// json return struct
type ResultMsg struct {
	Code   int64       `json:"code"`
	Msg    string      `json:"msg"`
	Result interface{} `json:"result"`
}